package com.hiray.demos.glue

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.util.AttributeSet
import android.util.Log
import com.hiray.demos.ItemAdapter
import com.hiray.demos.R

/**
 * Created by hiray on 2018/6/25.
 * a static rv for fast creating a List
 *@author hiray
 */
class RecyclerViewEx : androidx.recyclerview.widget.RecyclerView {
    val TAG = "RecyclerViewEx"
    private val attrArray = intArrayOf(R.attr.string_items)
    lateinit var mAdapter: ItemAdapter

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context!!, attrs, defStyle) {
        val ta = context!!.obtainStyledAttributes(attrs, attrArray)
        //R.attr.string_items index is 0 in attrArray
        if (ta.hasValue(0)) {
            val resourceId = ta.getResourceId(0, 0)
            Log.i(TAG, ": ")
            if (resourceId != 0) {
                val items = context.resources.getStringArray(resourceId)
                mAdapter = ItemAdapter(items.toList(), null)
                adapter = mAdapter
                layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context, VERTICAL, false)
            }
        }

        ta.recycle()
    }
}